﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Runtime.InteropServices;

namespace SoftwareCenter
{
    public class CommandCenter:CmdCenterInterface
    {


        //命令接口
        #region CmdCenterInterface Members
        public string ComingNumber
        {
            get;
            set;
        }
        public bool IsProcessRunning(string process_name)
        {
            try
            {
                var p = GetProcess(process_name);
                if (p != null && p.MainWindowHandle != null)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }
        private Process GetProcess(string process_name)
        {
            try
            {
                string path = Tool.GetProfileString(process_name);

                path = System.IO.Path.GetDirectoryName(path) + @"\config.txt";
                int processId = Convert.ToInt32(Tool.GetProfileString(path, "ProcessId"));
                var p = Process.GetProcessById(processId);
                return p;
            }
            catch
            {
                return null;
            }
        }

        public bool KillProcess(string process_name)
        {
            try
            {
                GetProcess(process_name).Kill();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool StartProcess(string process_name)
        {
            try
            {
                string path = Tool.GetProfileString(process_name);
                Process.Start(path, "");
                return true;
            }
            catch
            {
                return false;
            }
        }

        public List<string> GetProcessList()
        {
            List<string> strs = new List<string>();
            //Process.GetProcessById(
            return null;
        }


        public bool Restart()
        {
            return Restarter.Restart();
        }
        #endregion


    }
}
